<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>root管理员首页</title>
    <!-- <link href="http://cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"> -->
    <link href="../css/bootstrap/bootstrap.min.css" rel="stylesheet">
    <link href="../css/global.css" rel="stylesheet">
</head>

<body>
	<!--1.0 narbar -->
    <div   class="fixedNavbar">
       <#include "../navbar.html">
	</div>
    
    <div class="container-fluid">
    	<!--2.0 left sidebar -->
        <div class="row">
            <#include "../sidebar.html">

            <!--2.1 main content -->
            <div class="col-md-10">
		            <ul id="myTab" class="nav nav-tabs" role="tablist">
		                <li class="active"><a href="#fee-manage" role="tab" data-toggle="tab">&nbsp;公司列表</a>
		                </li>
		            </ul>
		            <div class="tab-pane" id="companies">
                        <div class="panel panel-default" id="companiesList">
				            <div id="myTabContent" class="tab-content" style="margin-top:10px;">
				                <div class="tab-pane active">
				                    <div class="panel panel-default">
				                        <div class="panel-heading">
				                            <form class="form-inline" role="form" id="searchForm">
				                                <div class="form-group">
				                                  <input type="text" class="form-control" id="keyword" placeholder="公司名/联系人姓名">
				                                </div>
				                                <button type="submit" class="btn btn-primary">查询</button>
				                            </form>
				                        </div>
				                    </div>
				                </div>
				            </div>
				            <table class="table table-striped table-bordered table-hover">
		                        <thead>
		                            <tr>
		                                <th>公司名称</th>
		                                <th>联系人姓名</th>
		                                <th>联系电话</th>
		                                <th>注册时间</th>
		                                <th>操作</th>
		                            </tr>
		                        </thead>
		                        <tbody id="dataList">
		                            <tr>
		                                <td>广州市白云区abc公司</td>
		                                <td>李志</td>
		                                <td>020-1234567</td>
		                                <td>2017.5.10</td>
		                                <td style="text-align:center;">
		                                	<a href="#" class="editCompany">修改</a>&nbsp;&nbsp;
		                                	<!-- <a href="#" class="delCompany">删除</a> -->
		                                </td>
		                            </tr>
		                            <tr>
		                                <td>上海市白云区123公司</td>
		                                <td>020-1234567</td>
		                                <td>2017.5.10</td>
		                                <td style="text-align:center;">
		                                	<a href="#" class="editCompany">修改</a>&nbsp;&nbsp;
		                                	<!--<a href="#" class="delCompany">删除</a> -->
		                                </td>
		                            </tr>
		                        </tbody>
		                    </table>
		                    <nav>
							  	<div class="row" style="text-align:center">
				                    <ul id="example" class="pagination">
				                    </ul>
				                </div>
							</nav>
						</div>
							
							<!--编辑操作员 -->
                        <div class="panel panel-default" id="editCompanyPanel" style="display:none;">
                            <div class="panel-heading" style = "font-weight:bold;">编辑公司信息</div>
                            <form id="editCompanyForm" class="form-horizontal" role="form" style="margin-top:16px;">
                            	<span id="id" style="display:none"></span>
                                <div class="form-group">
                                    <label for="companyName" class="col-sm-2 control-label">公司名</label>
                                    <div class="col-sm-3">
                                        <input class="form-control" name="companyName" id="companyName" readonly>
                                    </div>
                                    <span class="col-xs-6 help-block" style="color:red;"></span>
                                </div>
                                <div class="form-group">
                                    <label for="contactor_realname" class="col-sm-2 control-label">联系人姓名</label>
                                    <div class="col-sm-3">
                                        <input class="form-control" name="contactor_realname" id="contactor_realname" placeholder="联系人">
                                    </div>
                                    <span class="col-xs-6 help-block" style="color:red;"></span>
                                </div>
                                <div class="form-group">
                                    <label for="contactor_landline" class="col-sm-2 control-label">联系固话</label>
                                    <div class="col-sm-3">
                                        <input class="form-control" name="contactor_landline" id="contactor_landline" placeholder="操作员一">
                                    </div>
                                    <span class="col-xs-6 help-block" style="color:red;"></span>
                                </div>
                               
                                <div class="form-group">
                                    <label for="createTime" class="col-sm-2 control-label">注册时间</label>
                                    <div class="col-sm-3">
                                        <input class="form-control" name="createTime" id="createTime" readonly>
                                    </div>
                                    <span class="col-xs-6 help-block" style="color:red;"></span>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-offset-2 col-sm-3">
                                        <button type="submit" id="btnSaveCompany" class="btn btn-primary">保存</button>
                                    </div>
                                </div>
                            </form>
                        </div><!--end 编辑操作员-->

            		</div>
        	</div>
    	</div>
    </div>
    <script src="../js/bootstrap/jquery-1.11.1.min.js"></script>
    <script src="../js/bootstrap/bootstrap.min.js"></script>
    <script src="../js/bootstrap-paginator.js"></script>
    <script src="../js/_x_ctr_location-1.0.min.js"></script>
    <script src="../js/jquery.iframe-transport.js"></script>
    <script src="../js/jquery.validate.js"></script>
    <script src="../js/jquery.tablesorter.min.js"></script>
    <script src="../js/common.js"></script>
    <script src="../js/common/pageConfig.js?v=12312432"></script>
    <script src="../js/common/pageUtil.js"></script>
	<script src="../js/util.js"></script>
	<script src="../js/sha1.js"></script>
	<script src="../js/bootstrap-dialog.min.js"></script>
    
    <script>
    	$(function(){
        	//加载数据
        	var params= {
       			pageNo:Page.getConstant('PAGE_NO'),
       			pageSize:Page.getConstant('PAGE_SIZE'),
       			companyType: "COMPANY",
       			companyName: ""
        	};
        	loadData(params);
            
        	//模糊搜索
        	$("#searchForm").validate({
				rules: {
					keyword: {
						required : true,
					    minlength: 2,
					    maxlength: 100
					}
				},
				messages: {
					keyword: {
						required : "名称不能为空",
						minlength : "名称太短",
						maxlength : "名称太长,请检查"
					}
				},
				submitHandler : function(form) {
					params.keyword = $("#keyword").val();
				 	loadData(params);
				}
			});
        	
        	//处理修改界面的保存按钮
        	$.validator.addMethod("isLandlineNum", function(value, element){     
		        var pattern =/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{1,}))?$/;  
		        if(value != '') {
		        	if(!pattern.exec(value)) {
		        		return false;
		        	}
		        }
		        return true;   
		    });
			
			$("#editCompanyForm").validate({
				rules: {
					contactor_realname: {
						required: true,
					    minlength: 2,
						maxlength: 20
					},
					contactor_landline: {
						required: true,
						isLandlineNum: ""
					},
				},
				messages: {
					contactor_realname: {
						required: "联系人不能为空",
					    minlength: "请输入不小于2个字符",
						maxlength: "请输入不超过20个字符"
					},
					contactor_landline: {
						required: "固话不能为空",
						isLandlineNum: "请输入正确的固话号码"
					},
				},
				submitHandler : function(form) {
					debugger;
					$("#register-hint").text("正在运行,请稍后...").show();
					var companyName = $("#companyName").val();
					var contactor_realname = $("#contactor_realname").val();
					var contactor_landline = $("#contactor_landline").val();
					
					var url = "/eoip/web/company/updateCompany";
					var params = {  
									id: $('#id').val(),
									companyName:companyName,
									contactor:contactor_realname,
									contactorLandline:contactor_landline,
									};
					
				 	common.post(url, params, function (data) {
                        var data = jQuery.parseJSON(data);
                        if (data.code == "1") {
                            alert('保存成功!');
                            location.href = '/eoip/web/company_manage';
                        } else {
                        	alert(data.info);
                        }//end if
                    });//end common post
				}
			});
        	
            //处理修改按钮事件
            $("#dataList").on("click",".editCompany",function(){
            	
            	//1. hide & show trick
            	$("#companiesList").hide();
            	$("#editCompanyPanel").show();
            	
            	//2. fill edit company form 
            	var url = "/eoip/web/company/getCompanyByName";
            	var params= {
            		companyName: $(this).parent().prevAll(".companyName").html(),
            	};
            	common.post(url, params, function(data){
            		data = $.parseJSON(data);
            		if(data.code == 1) {
            			$('#id').val(data.company.id);
                		$('#companyName').val(data.company.companyName);
                		$('#contactor_realname').val(data.company.contactor);
                		$('#contactor_landline').val(data.company.contactorLandline);
                		$('#createTime').val(data.company.createTime);
            		} else {
            			alert(data.info);
            		}
            	});
            });
            
            
    	});
    	
    	//分页插件options
    	var options = {
		  	bootstrapMajorVersion: 3, //版本
            currentPage: Page.getConstant('PAGE_NO'), //当前页数
            totalPages: Page.getConstant('PAGE_SIZE'), //总页数
            itemTexts: function (type, page, current) {
              switch (type) {
                case "first":
                  return "首页";
                case "prev":
                  return "上一页";
                case "next":
                  return "下一页";
                case "last":
                  return "末页";
                case "page":
                  return page;
              }
            },//点击事件，用于通过Ajax来刷新整个list列表
            onPageChanged:onPageChanged
        };//end option
        
        function onPageChanged(event, oldPage, newPage){
        	loadData({
    			pageNo:newPage,
    			pageSize:Page.getConstant('PAGE_SIZE'),
    		});
        }
        
        function fillData(data){
        	$.each(data,function(index,item){
        		var html="";
        		html=html+
        			'<tr >'+
        				'<td class="companyId" style="display:none;">'+item.id+'</td>'+
            			'<td class="companyName">'+item.companyName+'</td>'+
            			'<td class="contactor">'+item.contactor+'</td>'+
            			'<td>'+item.contactorLandline+'</td>'+
            			'<td>'+ item.createTime + '</td>'+
            			'<td style="text-align:center;">' + 
            				'<a href="#"' +  'class="editCompany btn btn-success btn-xs">修改</a>&nbsp;&nbsp;'+
            				//'<a href="#"' +  'class="delCompany btn btn-danger btn-xs">删除</a>&nbsp;&nbsp;</td>'+
        			'</tr>';
        			$("#dataList").append(html);
			});
        }
        
        function loadData(params){
        	$("#dataList").empty();
            common.post('/eoip/web/company/getCompanyListByPage',params,function(data){
            	   data = $.parseJSON(data);
             	   var pageCount = data.totalPage;
         		   var currentPage = data.pageNo;
         		   
         		   options.currentPage=currentPage;
         		   options.totalPages=pageCount;
         		   
         		   fillData(data.dataList);
         	       $('#example').bootstrapPaginator(options);
             });
        }//end loadData
    </script>

</body>
</html>
